package com.zbbt.sportslottery.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.bean.News;
import com.zbbt.sportslottery.bean.NewsStatus;
import com.zbbt.sportslottery.db.DbManager;
import com.zbbt.sportslottery.ui.adapter.HomeRecyclerAdapter.ItemViewHolder2;
import com.zbbt.sportslottery.ui.adapter.HomeRecyclerAdapter.ItemViewHolder4;
import com.zbbt.sportslottery.ui.adapter.HomeRecyclerAdapter.ItemViewHolder5;
import com.zbbt.sportslottery.ui.adapter.HomeRecyclerAdapter.ItemViewHolder6;
import com.zbbt.sportslottery.ui.widget.FooterRecyclerView;
import com.zbbt.sportslottery.ui.widget.HeadRecyclerView;
import com.zbbt.sportslottery.ui.widget.recycler.MyItemClickListener;
import com.zbbt.sportslottery.ui.widget.recycler.RecyclerItemViewHolder;
import com.zbbt.sportslottery.util.TimeUtil;

public class RecyclerViewAdapter extends Adapter<RecyclerView.ViewHolder> {
	private static final int TYPE_HEADER = 0;
	private static final int TYPE_FOOTER = 2;

	private OnBottomClickListener clickLonbottomistener;
	private MyItemClickListener itemClickListener;
	private List<News> news = new ArrayList<News>();
	private LayoutInflater layoutInflater;
	private Context myContext;
	private HeaderRecyclerView headerRecyclerView;
	private FooteRecyclerView footerRecyclerView;

	public RecyclerViewAdapter(List<News> homeListData, Activity context) {
		this.myContext = context;
		layoutInflater = LayoutInflater.from(context);
		this.news = homeListData;
	}

	@Override
	public int getItemCount() {
		return news.size() + 2;
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return TYPE_HEADER;
		} else if (position == news.size() + 1) {
			return 10;
		} else {
			return news.get(position-1).getImgType();
		}
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int typeView) {
		switch (typeView) {
		case TYPE_HEADER:// 头部
			return new HeadRecyclerView(layoutInflater.inflate(R.layout.item_index_top, viewGroup, false), itemClickListener);
		case 2:// 纯文本
			return new ItemViewHolder2(layoutInflater.inflate(R.layout.item_only_text, viewGroup, false), itemClickListener);
		case 1:
		case 3:
		case 4:// 小图
			return new ItemViewHolder4(layoutInflater.inflate(R.layout.item_small_pic, viewGroup, false), itemClickListener);
		case 5:// 大图
			return new ItemViewHolder5(layoutInflater.inflate(R.layout.item_big_pic, viewGroup, false), itemClickListener);
		case 6:// 图集
			return new ItemViewHolder6(layoutInflater.inflate(R.layout.item_atlas, viewGroup, false), itemClickListener);
		case 10:// 底部
			return new BottomRecyclerView(layoutInflater.inflate(R.layout.fragment_index_bottom, viewGroup, false), itemClickListener);
		default:
			break;
		}
		return null;
	}

	@Override
	public void onBindViewHolder(ViewHolder viewHolder, int position) {
		if (position == 0 && viewHolder.getItemViewType() == TYPE_HEADER) {
			if (headerRecyclerView != null) {
				headerRecyclerView.onBindHeaderView(viewHolder, position);
			}
		} else if (position == news.size() + 1 && viewHolder.getItemViewType() == 10) {
			if (footerRecyclerView != null) {
				footerRecyclerView.onBottomHeaderView(viewHolder, position);
			}
		} else if(news!=null&&news.size()>0){
			News itemData = news.get(position -1);
			NewsStatus newsStatus = null;
			newsStatus = DbManager.getReadNews(itemData.getNewsId() + "");
			String strTimer;
			if (viewHolder instanceof ItemViewHolder2) {
				if (itemData != null) {
					strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
					((ItemViewHolder2) viewHolder).tTimer.setText(strTimer);
					((ItemViewHolder2) viewHolder).title.setText(itemData.getNewsTitle());
					((ItemViewHolder2) viewHolder).summary.setText(itemData.getNewsModel().replace("\r\n", ""));
					if (newsStatus != null) {
						((ItemViewHolder2) viewHolder).title.setTextColor(myContext.getResources().getColor(R.color.black_54));
						((ItemViewHolder2) viewHolder).summary.setTextColor(myContext.getResources().getColor(R.color.black_54));
					} else {
						((ItemViewHolder2) viewHolder).title.setTextColor(myContext.getResources().getColor(R.color.black_87));
						((ItemViewHolder2) viewHolder).summary.setTextColor(myContext.getResources().getColor(R.color.black_54));
					}
					((ItemViewHolder2) viewHolder).information_flag.setText(getNewsFlag(itemData.getNews_flag()));
				}
			} else if (viewHolder instanceof ItemViewHolder4) {
				if (itemData != null) {
					((ItemViewHolder4) viewHolder).title.setText(itemData.getNewsTitle());
					strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
					if (getNewsFlag(itemData.getNews_flag()).equals("")) {
						((ItemViewHolder4) viewHolder).information_time.setText(strTimer);
					} else {
						((ItemViewHolder4) viewHolder).information_time.setText(strTimer + "  " + getNewsFlag(itemData.getNews_flag()));
					}
					if (newsStatus != null) {
						((ItemViewHolder4) viewHolder).title.setTextColor(myContext.getResources().getColor(R.color.black_54));
						((ItemViewHolder4) viewHolder).information_time.setTextColor(myContext.getResources().getColor(R.color.black_54));
					} else {
						((ItemViewHolder4) viewHolder).title.setTextColor(myContext.getResources().getColor(R.color.black_87));
						((ItemViewHolder4) viewHolder).information_time.setTextColor(myContext.getResources().getColor(R.color.black_54));
					}
					if (null != itemData.getNewsImg() && itemData.getNewsImg().size() > 0) {
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(0).getImgUrl() + "", ((ItemViewHolder4) viewHolder).smallPic, App.option);
						((ItemViewHolder4) viewHolder).smallPic.setTag(itemData.getNewsImg().get(0).getImgUrl());
					} else {
						((ItemViewHolder4) viewHolder).smallPic.setImageResource(R.drawable.img_load_fail);
					}
				}
			} else if (viewHolder instanceof ItemViewHolder5) {
				if (itemData != null) {
					((ItemViewHolder5) viewHolder).title.setText(itemData.getNewsTitle());
					strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
					if (getNewsFlag(itemData.getNews_flag()).equals("")) {
						((ItemViewHolder5) viewHolder).bigTimer.setText(strTimer);
					} else {
						((ItemViewHolder5) viewHolder).bigTimer.setText(strTimer + "  " + getNewsFlag(itemData.getNews_flag()));
					}
					if (newsStatus != null) {
						((ItemViewHolder5) viewHolder).title.setTextColor(myContext.getResources().getColor(R.color.black_54));
					} else {
						((ItemViewHolder5) viewHolder).title.setTextColor(myContext.getResources().getColor(R.color.black_87));
					}
					if (null != itemData.getNewsImg() && itemData.getNewsImg().size() > 0) {
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(0).getImgUrl() + "", ((ItemViewHolder5) viewHolder).bitPic, App.option);
						((ItemViewHolder5) viewHolder).bitPic.setTag(itemData.getNewsImg().get(0).getImgUrl());
					} else {
						((ItemViewHolder5) viewHolder).bitPic.setImageResource(R.drawable.img_load_failbig);
					}
				}
			} else if (viewHolder instanceof ItemViewHolder6) {
				if (itemData != null) {
					((ItemViewHolder6) viewHolder).title.setText(itemData.getNewsTitle());
					strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
					if (getNewsFlag(itemData.getNews_flag()).equals("")) {
						((ItemViewHolder6) viewHolder).atTimer.setText(strTimer);
					} else {
						((ItemViewHolder6) viewHolder).atTimer.setText(strTimer + "  " + getNewsFlag(itemData.getNews_flag()));
					}
					if (newsStatus != null) {
						((ItemViewHolder6) viewHolder).title.setTextColor(myContext.getResources().getColor(R.color.black_54));
					} else {
						((ItemViewHolder6) viewHolder).title.setTextColor(myContext.getResources().getColor(R.color.black_87));
					}
					if (null != itemData.getNewsImg() && itemData.getNewsImg().size() > 0) {
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(0).getImgUrl() + "", ((ItemViewHolder6) viewHolder).picLeft, App.option);
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(1).getImgUrl() + "", ((ItemViewHolder6) viewHolder).picMiddle, App.option);
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(2).getImgUrl() + "", ((ItemViewHolder6) viewHolder).picRight, App.option);
					}
				}
			}
		}
	}

	public String getNewsFlag(int num) {
		if (num == 1) {
			return "热";
		} else if (num == 2) {
			return "推荐";
		} else if (num == 3) {
			return "专家";
		}
		return "";
	}

	// ////////////////////////////////////////////////////////////////////////////////
	public interface HeaderRecyclerView {
		public void onBindHeaderView(RecyclerView.ViewHolder holder, int position);
	}

	public interface FooteRecyclerView {
		public void onBottomHeaderView(RecyclerView.ViewHolder holder, int position);
	}

	class BottomRecyclerView extends RecyclerItemViewHolder {
		public TextView index_bottom;

		public BottomRecyclerView(View view, MyItemClickListener clickListener) {
			super(view, clickListener);
			index_bottom = (TextView) view.findViewById(R.id.index_bottom);
		}
	}

	class moreRecyclerView extends RecyclerItemViewHolder {
		TextView title;
		ImageView smallPic;
		TextView information_time;

		public moreRecyclerView(View view, MyItemClickListener clickListener) {
			super(view, clickListener);
			title = (TextView) view.findViewById(R.id.information_small_pic_title);
			smallPic = (ImageView) view.findViewById(R.id.information_small_pic);
			information_time = (TextView) view.findViewById(R.id.information_time);
		}

	}

	public interface OnBottomClickListener {
		public void onButtomClick();
	}

	public void setOnBottomClickListener(OnBottomClickListener clickListener) {
		this.clickLonbottomistener = clickListener;
	}

	/**
	 * 设置Item点击监听
	 * 
	 * @param listener
	 */
	public void setOnItemClickListener(MyItemClickListener listener) {
		this.itemClickListener = listener;
	}

	public void setOnBindHeadView(HeaderRecyclerView headerRecyclerView) {
		this.headerRecyclerView = headerRecyclerView;
	}

	public void setFootBinHeadView(FooteRecyclerView footeRecyclerView) {
		this.footerRecyclerView = footeRecyclerView;
	}
}
